Page({
  data: {
    id: null,
    formData: {
      name: '',
      price: '',
      description: ''
    },
    images: [],
    categories: [],
    selectedCategory: null,
    loading: true
  },
  
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
      this.getProductDetail(options.id);
      this.getCategories();
    } else {
      wx.navigateBack();
    }
  },
  
  getProductDetail: function(id) {
    wx.showLoading({
      title: '加载中...',
    });
    
    wx.request({
      url: `https://api.example.com/products/${id}`,
      method: 'GET',
      success: (res) => {
        wx.hideLoading();
        if (res.data.success) {
          const product = res.data.product;
          this.setData({
            formData: {
              name: product.name,
              price: product.price.toString(),
              description: product.description || ''
            },
            images: product.images || [],
            selectedCategory: product.category,
            loading: false
          });
        } else {
          wx.showToast({
            title: res.data.message || '获取商品详情失败',
            icon: 'none'
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    });
  },
  
  // 其他方法与添加商品页面相同，但submitForm和saveDraft需要修改为更新操作
  
  submitForm: function(e) {
    // ... 验证逻辑与添加商品相同 ...
    
    // 提交更新
    const productData = {
      name: formData.name,
      price: parseFloat(formData.price),
      description: formData.description,
      categoryId: this.data.selectedCategory.id,
      images: this.data.images,
      status: 'active'
    };
    
    wx.showLoading({
      title: '更新中...',
    });
    
    wx.request({
      url: `https://api.example.com/products/${this.data.id}`,
      method: 'PUT',
      data: productData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.success) {
          wx.showToast({
            title: '更新成功',
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.message || '更新失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  }
  
  // ... 其他方法与添加商品页面相同 ...
}); 